package com.wkbb.sports.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseController;
import com.wkbb.common.constants.ErrorCode;
import com.wkbb.common.constants.VersionConstant;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.StudentInfoDto;
import com.wkbb.common.dto.basic.UserDto;
import com.wkbb.common.handler.JsonUtil;
import com.wkbb.common.utils.StringUtil;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.model.*;
import com.wkbb.sports.service.*;
import com.wkbb.sports.utils.RandomUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.InputStream;
import java.util.*;

@RestController
@Api(description = "学生擂台")
@RequestMapping("/sports/{apiVersion}/student/arena")
@Slf4j
public class StudentArenaController extends BaseController {

    @Autowired
    private StudentArenaService service;
    @Autowired
    private BasicFeignService basicFeignService;

    @ApiOperation(value = "创建擂台")
    @PostMapping(value = "/addArena")
    public ResultDTO<StudentArena> addArena(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                              @Valid @RequestBody StudentArena studentArena) {
        log.info("***********StudentArenaController.addArena******apiVersion:{},info:{}", apiVersion,  JsonUtil.objectToJson(studentArena));
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (studentArena.getStartTime() == null) studentArena.setStartTime(new Date());
            if (studentArena.getEndTime().getTime() < studentArena.getStartTime().getTime()) {
                return ResultDTO.errorResult("结束时间必须控制在开始时间之后");
            }
            studentArena.setModelType(1);
            if (studentArena.getWay() == null) studentArena.setWay(1);
            if (studentArena.getFaceRecognition() == null) studentArena.setFaceRecognition(false);
            if (studentArena.getGenerateInviteCode() == null) studentArena.setGenerateInviteCode(false);
            if (studentArena.getGenerateInviteCode()){
                boolean b = true;
                while (b){
                    String inviteCode = RandomUtil.generateNumber(6);
                    Map<String, Object> params = new HashMap<>();
                    params.put("inviteCode",inviteCode);
                    if (service.getByMapParams(params) == null){
                        b = false;
                        studentArena.setInviteCode(inviteCode);
                    }
                }
            }else studentArena.setInviteCode(null);
            if (studentArena.getJumpType() == null) studentArena.setJumpType(1);
            studentArena.setOfficial(false);
            service.insert(studentArena);
            return ResultDTO.successResult(studentArena);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "创建官方擂台")
    @PostMapping(value = "/addOfficialArena")
    public ResultDTO<StudentArena> addOfficialArena(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                              @Valid @RequestBody StudentArena studentArena) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            studentArena.setOfficial(true);
            if (studentArena.getStartTime() == null) studentArena.setStartTime(new Date());
            service.addOfficialArena(studentArena);
            return new ResultDTO<>(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "参与个人赛邀请码校验")
    @PostMapping(value = "/validInviteCode")
    public ResultDTO<Boolean> validInviteCode(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                       @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                                       @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) String studentId,
                                                       @ApiParam(value = "邀请码", required = true) @RequestParam(name = "inviteCode", required = true) String inviteCode) {
        try {
            if (StringUtils.isEmpty(studentId)){
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_STU_NOT_EXIST);
            }
            if (StringUtils.isEmpty(inviteCode)){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return service.validInviteCode(arenaId,Long.parseLong(studentId),inviteCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "当前学生是否能进行亲子擂台挑战")
    @GetMapping(value = "/isFamilyArenaChange")
    public ResultDTO<StudentArenaTeam> isFamilyArenaChange(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                        @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                                        @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) String studentId) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (StringUtils.isEmpty(studentId)){
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_STU_NOT_EXIST);
            }
            return service.isFamilyArenaChange(arenaId,Long.parseLong(studentId),userDto.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "创建擂台 OR 亲子团队")
    @PostMapping(value = "/addArenaTeam")
    public ResultDTO<StudentArenaTeam> addArenaTeam(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                    @Valid @RequestBody StudentArenaTeam info) {
        log.info("***********StudentArenaController.addArenaTeam******apiVersion:{},info:{}", apiVersion,  JsonUtil.objectToJson(info));
        try {
            if (StringUtils.isEmpty(info.getArenaId()) || StringUtils.isEmpty(info.getStudentId()) || StringUtils.isEmpty(info.getTeamName()) ){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            info.setUserId(userDto.getId());
            return service.insertTeam(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "加入擂台团队")
    @PostMapping(value = "/joinArenaTeam")
    public ResultDTO<StudentArenaTeamStu> joinArenaTeam(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @Valid @RequestBody StudentArenaTeamStu info) {
        try {
            if (StringUtils.isEmpty(info.getArenaId()) || StringUtils.isEmpty(info.getArenaTeamId()) || StringUtils.isEmpty(info.getStudentId()) ){
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return service.joinArenaTeam(info);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "擂台团队移除学生")
    @PostMapping(value = "/removeArenaTeamStu")
    public ResultDTO<StudentArenaTeamStu> delArenaTeamStu(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                             @ApiParam(value = "擂台团队编号", required = true) @RequestParam(name = "arenaTeamId", required = true) Long arenaTeamId,
                                                             @ApiParam(value = "团队队长学生ID", required = true) @RequestParam(name = "captainStudentId", required = true) Long captainStudentId,
                                                             @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) Long studentId) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return service.delArenaTeamStu(arenaTeamId,captainStudentId,studentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取首页擂台列表")
    @GetMapping(value = "/getHomeList")
    public ResultDTO<List<StudentArena>> getHomeList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
        @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) Long studentId,
        @ApiParam(value = "经度", required = false) @RequestParam(name = "longitude", required = false) Double longitude,
        @ApiParam(value = "纬度", required = false) @RequestParam(name = "latitude", required = false) Double latitude) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            Map<String, Object> params = new HashMap<>();
            params.put("state",1);
            params.put("sortBy",0);
            params.put("showArea",0);
            params.put("longitude",longitude);
            params.put("latitude",latitude);
            int userType = 2;
            if (userDto!= null && studentId != null) {
                ResultDTO<StudentInfoDto> resultDTO = basicFeignService.getStudentByFamily(studentId,userDto.getId());
                if (resultDTO.isSuccess()){
                    StudentInfoDto studentInfoDto = resultDTO.getData();
//                params.put("gradeId",studentInfoDto.getGradeId());
//                params.put("showArea",studentInfoDto.getUserType() != 1?1:0);
//                log.debug("studentInfoDto",JsonUtil.objectToJson(studentInfoDto));
                    userType = studentInfoDto.getUserType();
                    if (studentInfoDto.getUserType() == 1){
                        if (studentInfoDto.getOrgId() > 0){
                            params.put("orgId",studentInfoDto.getOrgId());
                            params.put("classId",studentInfoDto.getClassId());
                        }else{
//                    params.put("orgName",studentInfoDto.getSchoolId());
                            params.put("schoolId",studentInfoDto.getSchoolId());
                            params.put("gradeName",studentInfoDto.getGradeName());
                            params.put("className",studentInfoDto.getClassName());
                        }
                    }
                }
            }
            params.put("userType",userType);
//            log.error("getHomeList:{}",params);
            PageInfo<StudentArena> pageInfo = service.getListByPage(params,1,4);
            return ResultDTO.successResult(pageInfo.getList());
//            return new ResultDTO<>(ErrorCode.FAMILY_STU_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取擂台列表")
    @GetMapping(value = "/getListByPage")
    public ResultDTO<PageInfo<StudentArena>> getListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) Long studentId,
                                      @ApiParam(value = "类型(1个人赛 2团队赛)", required = false) @RequestParam(name = "type", required = false) Integer type,
                                      @ApiParam(value = "是否官方发布", required = false) @RequestParam(name = "official", required = false) Boolean official,
                                      @ApiParam(value = "名称", required = false) @RequestParam(name = "name", required = false) String name,
                                      @ApiParam(value = "显示区域 0全部 1全民 2学校 3班级", defaultValue = "0", required = false) @RequestParam(name = "showArea", defaultValue = "0", required = false) Integer showArea,
                                      @ApiParam(value = "擂台状态 0未开始 1进行中 2已结束", required = false) @RequestParam(name = "state", required = false) Integer state,
                                      @ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Integer gradeId,
                                      @ApiParam(value = "经度", required = false) @RequestParam(name = "longitude", required = false) Double longitude,
                                      @ApiParam(value = "纬度", required = false) @RequestParam(name = "latitude", required = false) Double latitude,
                                      @ApiParam(value = "排序规则 1最新发起 2热门挑战 3即将结束", defaultValue = "1", required = false) @RequestParam(name = "sortBy", defaultValue = "1", required = false) Integer sortBy,
                                      @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
                                      @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            Map<String, Object> params = new HashMap<>();
            params.put("sortBy",sortBy);
            params.put("type",type);
            if (sortBy == 3) state = 1;
            params.put("state",state);
            params.put("gradeId",gradeId);
            params.put("showArea",showArea);
            params.put("longitude",longitude);
            params.put("latitude",latitude);
            params.put("name",name);
            if (official != null) params.put("official",official?1:0);
            int userType = 2;
            if (userDto!= null && studentId != null) {
                ResultDTO<StudentInfoDto> resultDTO = basicFeignService.getStudentByFamily(studentId, userDto.getId());
                if (resultDTO.isSuccess()) {
                    StudentInfoDto studentInfoDto = resultDTO.getData();
                    log.info("studentInfoDto:{}",JsonUtil.stringify(studentInfoDto));
//                params.put("gradeId",studentInfoDto.getGradeId());
                    userType = studentInfoDto.getUserType();
                    if (studentInfoDto.getOrgId() > 0){
                        params.put("orgId",studentInfoDto.getOrgId());
                        params.put("classId",studentInfoDto.getClassId());
                    }else{
                        params.put("schoolId",studentInfoDto.getSchoolId());
                        params.put("gradeName",studentInfoDto.getGradeName());
                        params.put("className",studentInfoDto.getClassName());
                    }
                }
            }else{
                params.put("orgId",-1);
                params.put("classId",-1);
            }
            params.put("userType",userType);
            PageInfo<StudentArena> pageInfo = service.getListByPage(params,pageNo,pageSize);
            return ResultDTO.successResult(pageInfo);
//            return new ResultDTO<>(ErrorCode.FAMILY_STU_ERROR);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取我的擂台列表")
    @GetMapping(value = "/getMyListByPage")
    public ResultDTO<PageInfo<StudentArena>> getMyListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) String studentId,
                                      @ApiParam(value = "类型(1我的挑战 2我的发起)", required = true) @RequestParam(name = "type", defaultValue = "1", required = true) Integer type,
                                      @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
                                      @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
            UserDto userDto = this.getRouteUserByUnion();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            if (StringUtils.isEmpty(studentId)){
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_STU_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("type",type);
            params.put("studentId",studentId);
            PageInfo<StudentArena> pageInfo = service.getMyListByPage(params,pageNo,pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "根据ID、邀请码获取擂台详情")
    @GetMapping(value = "/getArena")
    public ResultDTO<StudentArena> getArena(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "擂台ID", required = false) @RequestParam(name = "arenaId", required = false) Long arenaId,
                                      @ApiParam(value = "邀请码", required = false) @RequestParam(name = "inviteCode", required = false) String inviteCode,
                                      @ApiParam(value = "学生ID", required = false) @RequestParam(name = "studentId", required = false) Long studentId) {
        try {
//            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            if (null == arenaId && inviteCode == null) {
                return new ResultDTO<>(ErrorCode.PARAM_MISSING);
            }
            StudentArena data;
            if (arenaId != null){
                data = service.getByPrimaryKey(arenaId);
            }else {
                Map<String, Object> params = new HashMap<>();
                params.put("inviteCode",inviteCode);
                data = service.getByMapParams(params);
            }
            if (studentId != null && data != null){
                if (!studentId.equals(data.getStudentId())) data.setInviteCode(null);
                data.setTrain(service.getStuTrain(arenaId,studentId));
            }
            return ResultDTO.successResult(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取个人擂台成绩列表")
    @GetMapping(value = "/getStuListByPage")
    public ResultDTO<PageInfo<StudentArenaTrain>> getStuListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                      @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
                                      @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
//            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            return service.getStuListByPage(arenaId,pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取团队擂台成绩列表")
    @GetMapping(value = "/getTeamListByPage")
    public ResultDTO<List<StudentArenaTeam>> getTeamListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                                   @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId) {
        try {
//            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            List<StudentArenaTeam> pageInfo = service.getTeamListByPage(params);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取团队擂台选手成绩列表")
    @GetMapping(value = "/getTeamStuList")
    public ResultDTO<List<StudentArenaTrain>> getTeamStuList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                      @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                      @ApiParam(value = "擂台团队ID", required = true) @RequestParam(name = "arenaTeamId", required = true) Long arenaTeamId) {
        try {
//            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            params.put("arenaTeamId",arenaTeamId);
            return service.getTeamStuList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取擂台单个选手挑战列表")
    @GetMapping(value = "/getStuChallengeList")
    public ResultDTO<List<StudentArenaTrain>> getStuChallengeList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                                      @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                                                      @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) Long studentId) {
        try {
//            UserDto userDto = this.getRouteUserByUnion();
//            if (null == userDto) {
//                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
//            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            params.put("studentId",studentId);
            List<StudentArenaTrain> list = service.getStuChallengeList(params);
            return ResultDTO.successResult(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "后端获取个人擂台成绩列表")
    @GetMapping(value = "/getManageStuListByPage")
    public ResultDTO<PageInfo<StudentArenaTrain>> getManageStuListByPage(
            @ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
            @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
            @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name = "pageNo", defaultValue = "1", required = false) int pageNo,
            @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if(null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            return service.getStuListByPage(arenaId,pageNo,pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "后端获取团队擂台成绩列表")
    @GetMapping(value = "/getManageTeamListByPage")
    public ResultDTO<List<StudentArenaTeam>> getManageTeamListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                                     @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId) {
        try {
            UserDto userDto = this.getUserByLogin();
            if(null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            List<StudentArenaTeam> pageInfo = service.getTeamListByPage(params);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "后端获取擂台列表")
    @GetMapping(value = "/getManageListByPage")
    public ResultDTO<PageInfo<StudentArena>> getManageListByPage(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
        @ApiParam(value = "类型(1个人赛 2团队赛)", required = false) @RequestParam(name = "type", required = false) Integer type,
        @ApiParam(value = "是否官方发布", required = false) @RequestParam(name = "official", required = false) Boolean official,
        @ApiParam(value = "名称", required = false) @RequestParam(name = "name", required = false) String name,
        @ApiParam(value = "显示区域 0全部 1全民 2学校 3班级", defaultValue = "0", required = false) @RequestParam(name = "showArea", defaultValue = "0", required = false) Integer showArea,
        @ApiParam(value = "擂台状态 1进行中 2已结束", required = false) @RequestParam(name = "state", required = false) Integer state,
        @ApiParam(value = "年级ID", required = false) @RequestParam(name = "gradeId", required = false) Integer gradeId,
        @ApiParam(value = "排序规则 1最新发起 2热门挑战 3即将结束", defaultValue = "1", required = false) @RequestParam(name = "sortBy", defaultValue = "1", required = false) Integer sortBy,
        @ApiParam(value = "分页数", defaultValue = "1", required = false) @RequestParam(name= "pageNo", defaultValue = "1", required = false) int pageNo,
        @ApiParam(value = "分页大小", defaultValue = "10", required = false) @RequestParam(name = "pageSize", defaultValue = "10", required = false) int pageSize) {
        try {
            UserDto userDto = this.getUserByLogin();
            if(null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("gradeId",gradeId);
            params.put("type",type);
            params.put("sortBy",sortBy);
            if (sortBy == 3) state = 1;
            params.put("state",state);
            params.put("showArea",showArea);
            params.put("manager",1);
            params.put("name",name);
            if (official != null) params.put("official",official?1:0);
            PageInfo<StudentArena> pageInfo = service.getListByPage(params,pageNo,pageSize);
            return ResultDTO.successResult(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取团队擂台选手成绩列表")
    @GetMapping(value = "/getManageTeamStuList")
    public ResultDTO<List<StudentArenaTrain>> getManageTeamStuList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                                   @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                                                   @ApiParam(value = "擂台团队ID", required = true) @RequestParam(name = "arenaTeamId", required = true) Long arenaTeamId) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            params.put("arenaTeamId",arenaTeamId);
            return service.getTeamStuList(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

    @ApiOperation(value = "获取擂台单个选手挑战列表")
    @GetMapping(value = "/getManageStuChallengeList")
    public ResultDTO<List<StudentArenaTrain>> getManageStuChallengeList(@ApiParam(value = "接口版本", required = true, defaultValue = VersionConstant.V1) @PathVariable(value = "apiVersion", required = true) String apiVersion,
                                                                        @ApiParam(value = "擂台ID", required = true) @RequestParam(name = "arenaId", required = true) Long arenaId,
                                                                        @ApiParam(value = "学生ID", required = true) @RequestParam(name = "studentId", required = true) Long studentId) {
        try {
            UserDto userDto = this.getUserByLogin();
            if (null == userDto) {
                return new ResultDTO<>(ErrorCode.SECURITY_ACCESS_TOKEN_NOT_EXIST);
            }
            Map<String, Object> params = new HashMap<>();
            params.put("arenaId",arenaId);
            params.put("studentId",studentId);
            List<StudentArenaTrain> list = service.getStuChallengeList(params);
            return ResultDTO.successResult(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ResultDTO<>(false);
    }

}
